package com.zz.forum.controller;

import com.sun.istack.internal.NotNull;
import com.zz.forum.common.AppResult;
import com.zz.forum.common.ResultCode;
import com.zz.forum.model.User;
import com.zz.forum.utils.MD5Util;
import com.zz.forum.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户接口
 */

@RequestMapping("/user")
@RestController
@Mapper
public class UserController {
    @PostMapping("/register")
    public AppResult createNormalUser(@ApiParam("昵称") @RequestParam("nikeName") @NotNull String nickName,
                                      @ApiParam("用户名") @RequestParam("username") @NotNull String username,
                                      @ApiParam("用户密码") @RequestParam("password") @NotNull String password,
                                      @ApiParam("重复密码") @RequestParam("passwordRepeat") @NotNull String passwordRepeat) {

        //校验两次密码是否相同
        if(!password.equals(passwordRepeat)){
            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }
        User user = new User();
        user.setUsername(username);
        
        //对密码进行加密
        //盐
        String salt = UUIDUtils.UUID_32();
        //加密后的密码
        String ciphertext = MD5Util.md5Salt(password, salt);
        user.setSalt(salt);
        user.setPassword(ciphertext);

        return AppResult.success("注册成功");

    }
}
